package com.neulogistics.contrib.transfercenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.transfercenter.api.application.command.GetSourceWarehouseIdCommand;
import com.neulogistics.contrib.transfercenter.domain.model.aggregate.TransferOrder;
import com.neulogistics.contrib.transfercenter.domain.repository.ITransferOrderRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class GetSourceWarehouseIdCommandHandler implements IRequestHandler<GetSourceWarehouseIdCommand, BaseResponse<Integer>> {

    @Resource
    ITransferOrderRepository transferOrderRepository;


    @Override
    public BaseResponse<Integer> handle(GetSourceWarehouseIdCommand command) throws Exception {
        TransferOrder transferOrder = transferOrderRepository.findById(command.transferId);
        Integer result = 0;
        if(command.target == 0){
            result = transferOrder.getOutboundWarehouseId();
        }
        else {
            result = transferOrder.getInboundWarehouseId();
        }
        return ResultUtils.success(result);
    }
}
